package com.kaka.bus.engine.borrow.person;

import android.app.Activity;

import com.kaka.bus.bean.City;
import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.di.DataManager;
import com.kaka.bus.engine.RouterUtil;
import com.kaka.bus.manager.Result;
import com.kaka.bus.manager.T;
import com.kaka.bus.mvp.RxPresenter;
import com.kaka.bus.util.U;
import com.kaka.bus.util.UserUtil;

import java.util.Map;

import javax.inject.Inject;

/**
 * Created by zhangqi on 2017/12/21.
 * e-mail : ${email}
 * desc :
 */

public class PersonPresenter extends RxPresenter<PersonContract.View> implements PersonContract.Presenter {

    @Inject
    public PersonPresenter(DataManager dataManager) {
        super(dataManager);
    }

    @Override
    public void sendService(Map<String, String> map,String token) {
        mView.showDialog("提交信息中..");
        addSubscribe(mDataManager
                .getApi()
                .commitPersonInfo(map,token,U.Sign(map,token))
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<String>>() {
                    @Override
                    protected void onSuccess(HttpWrapper<String> bean) {
                        mView.sendSuccess(bean);
                    }

                    @Override
                    protected void onFinish() {
                        super.onFinish();
                        mView.dissDialog();
                    }
                }));
    }

    @Override
    public void getCity() {
        /**
         * 查一下缓存
         */
        City city = UserUtil.getCache(UserUtil.CITYINFO);
        if(city != null){
            mView.readCitySuccess(city);
            return;
        }

        addSubscribe(mDataManager
                .getApi()
                .getCity()
                .compose(T.D())
                .subscribeWith(new Result<City>() {

                    @Override
                    protected void onSuccess(City city) {
                        mView.readCitySuccess(city);
                    }

                    @Override
                    protected void onFinish() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        mView.readCityError(t);
                    }
                }));
    }

    @Override
    public void router(Activity activity) {
        mView.showDialog("查询节点中...");
        RouterUtil.router(activity, mDataManager.getApi(),mCompositeDisposable, () -> {
            if(mView != null){
                mView.dissDialog();
            }
        });
    }

}
